<?php
/**
 * @file
 */
/**
 * Menu callback; Provide the live chat administration overview page.
 */
function chatroom_livesupport_admin_page() {
  // Check for status report errors.
  if (user_access('administer site configuration')) {
    drupal_set_message(t('One or more problems were detected with your Drupal installation. Check the <a href="@status">status report</a> for more information.', array('@status' => url('admin/reports/status'))), 'error');
  }
  $blocks = array();
  if ($admin = db_query("SELECT menu_name, mlid FROM {menu_links} WHERE link_path = 'admin/livesupport' AND module = 'system'")->fetchAssoc()) {
    $result = db_query("
      SELECT m.*, ml.*
      FROM {menu_links} ml
      INNER JOIN {menu_router} m ON ml.router_path = m.path
      WHERE ml.link_path <> 'admin/help' AND menu_name = :menu_name AND ml.plid = :mlid AND hidden = 0", $admin, array('fetch' => PDO::FETCH_ASSOC));

    foreach ($result as $item) {
      _menu_link_translate($item);
      if (!$item['access']) {
        continue;
      }
      // The link description, either derived from 'description' in hook_menu()
      // or customized via menu module is used as title attribute.
      if (!empty($item['localized_options']['attributes']['title'])) {
        $item['description'] = $item['localized_options']['attributes']['title'];
        unset($item['localized_options']['attributes']['title']);
      }
      $block = $item;
      $block['content'] = $item['description'];
      $block['content'] .= theme('admin_block_content', array('content' => system_admin_menu_block($item)));
      if (!empty($block['content'])) {
        $block['show'] = TRUE;
      }

      // Prepare for sorting as in function _menu_tree_check_access().
      // The weight is offset so it is always positive, with a uniform 5-digits.
      $blocks[(50000 + $item['weight']) . ' ' . $item['title'] . ' ' . $item['mlid']] = $block;
    }
  }
  if ($blocks) {
    ksort($blocks);
    return theme('admin_page', array('blocks' => $blocks));
  }
  else {
    return t('You do not have any administrative items.');
  }
}

/**
 * Menu callback: Configuration
 */
function chatroom_livesupport_config_form($form, &$form_state) {
  global $user, $language;

  $roles = user_roles();
  $admin_role_rid = variable_get('user_admin_role', FALSE);

  if ($admin_role_rid) {
    unset($roles[$admin_role_rid]);
  }

  $form = array(
    'chatroom_livesupport' => array(
      '#type' => 'fieldset',
      '#title' => 'Configuration',
      '#collapsible' => TRUE,
      'chatroom_livesupport_roles' => array(
        '#type' => 'select',
        '#multiple' => TRUE,
        '#title' => t('Which roles have use access to this live chat'),
        '#options' => $roles,
        '#default_value' => variable_get('chatroom_livesupport_roles', array()),
      ),
      'chatroom_livesupport_operator_role' => array(
        '#type' => 'select',
        '#multiple' => FALSE,
        '#title' => t('Which role have add to operator live chat'),
        '#options' => $roles,
        '#default_value' => variable_get('chatroom_livesupport_operator_role', array()),
      ),
      'chatroom_livesupport_expired_chatroom' => array(
        '#type' => 'textfield',
        '#default_value' => variable_get('chatroom_livesupport_expired_chatroom', ''),
        '#title' => t('Expired timeout for chatroom (seconds)'),
        '#description' => t('The expired time which chatrooms auto close. Leave it blank don\'t auto close chatroom.')
      )
    ),
  );
  if ($formats = filter_formats($user)) {
    $format_options = array();
    foreach ($formats as $format) {
      $format_options[$format->format] = $format->name;
    }
    $form['chatroom_livesupport']['chatroom_livesupport_msg_format'] = array(
      '#type' => 'select',
      '#title' => t('Message output format'),
      '#options' => $format_options,
      '#default_value' => variable_get('chatroom_livesupport_msg_format', 'plain_text'),
    );
  }
  $form['chatroom_livesupport']['chatroom_livesupport_notify_operator'] = array(
    '#type' => 'checkbox',
    '#title' => t('Notify offline operators.'),
    '#default_value' => variable_get('chatroom_livesupport_notify_operator', FALSE),
    '#description' => t('When enabled, the operators are offline will receive an e-mail notification after visitors have been required support.'),
  );
  $form['chatroom_livesupport']['chatroom_livesupport_notify_visitor'] = array(
    '#type' => 'checkbox',
    '#title' => t('Notify offline visitors.'),
    '#default_value' => variable_get('chatroom_livesupport_notify_visitor', FALSE),
    '#description' => t('When enabled, the visitors are offline will receive an e-mail notification after operators have been joined support.'),
  );

  $tokens = array(
    '#theme' => 'token_tree',
    '#token_types' => array('chatroom', 'chatroom-user'),
    '#global_types' => TRUE,
    '#click_insert' => TRUE,
    '#group' => 'email',
  );

  $form['email'] = array(
    '#type' => 'vertical_tabs',
  );

  $form['email_notify_operator'] = array(
    '#type' => 'fieldset',
    '#title' => t('Notification e-mail to operator'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#description' => t('Edit the notification e-mail messages sent to operator created when visitor require support.'),
    '#group' => 'email',
  );

  $form['email_notify_operator']['chatroom_livesupport_email_notify_operator_subject'] = array(
    '#type' => 'textfield',
    '#title' => t('Subject'),
    '#default_value' => _chatroom_livesupport_mail_text('notify_operator_subject', NULL, array(), FALSE),
    '#maxlength' => 180,
  );

  $form['email_notify_operator']['chatroom_livesupport_email_notify_operator_body'] = array(
    '#type' => 'textarea',
    '#title' => t('Body'),
    '#default_value' => _chatroom_livesupport_mail_text('notify_operator_body', NULL, array(), FALSE),
    '#rows' => 15,
  );

  $form['email_notify_operator']['tokens'] = $tokens;

  $form['email_notify_visitor'] = array(
    '#type' => 'fieldset',
    '#title' => t('Notification e-mail to visitor'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#description' => t('Edit the notification e-mail messages sent to visitor when operator has been access chatroom.'),
    '#group' => 'email',
  );

  $form['email_notify_visitor']['chatroom_livesupport_email_notify_visitor_subject'] = array(
    '#type' => 'textfield',
    '#title' => t('Subject'),
    '#default_value' => _chatroom_livesupport_mail_text('notify_visitor_subject', NULL, array(), FALSE),
    '#maxlength' => 180,
  );

  $form['email_notify_visitor']['chatroom_livesupport_email_notify_visitor_body'] = array(
    '#type' => 'textarea',
    '#title' => t('Body'),
    '#default_value' => _chatroom_livesupport_mail_text('notify_visitor_body', NULL, array(), FALSE),
    '#rows' => 15,
  );
  $form['email_notify_visitor']['tokens'] = $tokens;
  return system_settings_form($form);
}